public class Solution {
    public static int hammingWeight(int n) {
        int count = 0;
        while (n != 0) {
            // 每次将 n 最右边的 1 变为 0
            n &= (n - 1);
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        // 测试示例
        System.out.println(hammingWeight(11)); // 输出 3
        System.out.println(hammingWeight(128)); // 输出 1
        System.out.println(hammingWeight(2147483645)); // 输出 30
    }
}
